<template>
  <div class="v-property-item-flow g-relative">
    <div class="_table-body">
      <div
        class="g-relative g-pd-l-16"
        style="height: 100%"
      >
        <div class="g-flex _table-row g-ov-hidden _table-title">
          <div class="g-flex-1 _cell _name-column">
            {{ $t("名称") }}
          </div>
          <div class="g-flex-1 _cell _name-column">
            {{ $t("传递分类器") }}
          </div>
          <div class="g-flex-1 _cell _name-column">
            {{ $t("方向") }}
          </div>
          <div class="g-flex-1 _cell _value-column">
            {{ $t("项属性") }}
          </div>
        </div>
        <!-- 列表展示 -->
        <div v-if="itemFlowData && itemFlowData.length > 0">
          <div
            v-for="(row, index) of itemFlowData"
            :key="index"
            class="g-flex _table-row g-ov-hidden"
            :class="{ '_current-row': `${row.flow.id}-${index}` === curRowId }"
            @click="handleCilckRow(row, index)"
          >
            <div
              v-show="`${row.flow.id}-${index}` === curRowId"
              class="_table-body-icon"
            >
              <img
                src="statics/icons/app/arrowchoose.svg"
                class="g-m-r-4"
              />
            </div>
            <div
              class="g-flex-1 _cell _name-column"
              @contextmenu="$event => contextMenu($event, row.flow.id)"
            >
              <img
                :src="row.flow.icon"
                class="g-m-r-4"
              />
              {{ row.flow.name }}
            </div>
            <div
              class="g-flex-1 _cell _name-column"
              @contextmenu="$event => contextMenu($event, row.conveyedItems[0].id)"
            >
              <div
                v-if="row.conveyedItems.length"
                :key="index"
              >
                <img
                  :src="row.conveyedItems[0].icon"
                  class="g-m-r-4"
                />
                {{ row.conveyedItems[0].name }}
              </div>
            </div>
            <div class="g-flex-1 _cell _name-column">
              {{ row.flowDirection.name }}
            </div>
            <div
              class="g-flex-1 _cell _value-column"
              @contextmenu="$event => contextMenu($event, row.itemProperty.id)"
            >
              <img
                :src="row.itemProperty.icon"
                class="g-m-r-4"
              />
              {{ row.itemProperty.name }}
            </div>
          </div>
        </div>
        <div v-if="!itemFlowData || itemFlowData.length == 0">
          <NoData></NoData>
        </div>
      </div>
    </div>
    <div class="_footer-button">
      <el-button
        class="g-m-r-8"
        :disabled="isModelDisabled"
        @click="handleAdd"
      >
        <i class="el-icon-iconuse"></i>
        {{ $t("添加") }}
      </el-button>
      <el-button
        class="g-m-r-8"
        :disabled="!currentRow || isModelDisabled"
        @click="handleDelete"
      >
        <i class="el-icon-icondisuse"></i>
        {{ $t("移除") }}
      </el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, inject, ref, Ref } from "vue";

import NoData from "./noData.vue";
import { FlowFormData } from "@/modules/model/modals/createItemFlow/ItemFlowEditorModel";
import { modelService } from "@/modules/model/service";
import { propertyModelContextMenu } from "./propertyEditors/propertyUtil";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const props = defineProps<{
  itemFlowData: any[];
  modelId?: string;
}>();

const itemFlowPaneModel = app.activeProject!.attributeTabsModel.itemFlowPaneModel;
const curRowId = ref<string | undefined>();
const currentRow = computed(() => {
  return itemFlowPaneModel.tableData.find((it, index) => `${it.flow.id}-${index}` === curRowId.value);
});
const isModelDisabled = inject("isModelDisabled") as Ref<boolean>;

async function handleAdd() {
  await itemFlowPaneModel.handleAdd();
  // await itemFlowPaneModel.fresh();
}
async function handleDelete() {
  if (!currentRow.value) {
    return;
  }
  // 将传递对象从该项流中移除
  await itemFlowPaneModel.handleRemoveConvey(currentRow.value);
  // await itemFlowPaneModel.fresh();
}

function handleCilckRow(row: FlowFormData, index: number) {
  console.log("property---", row);
  curRowId.value = `${row.flow.id}-${index}`;
  console.log(curRowId);
}

async function contextMenu(event: MouseEvent, modelId: string) {
  if (!modelId) return;
  const model = await modelService.getModelData(app.activeProject!.id, modelId);
  propertyModelContextMenu(event, model);
}
</script>
<style lang="scss">
@import "@/assets/css/theme.scss";
.v-property-item-flow {
  height: 100%;
  overflow: hidden;

  display: flex;
  ._table-body-icon {
    // display: inline-flex;
    position: absolute;
    left: 4px;
    margin-top: 4px;
  }
  flex-direction: column;
  ._table-title {
    background: $titlebar-bg-color;
  }
  ._table-body {
    // flex: 1;
    height: calc(100% - 40px);
    overflow: auto;
    // max-height: 380px;
  }
  ._active-row {
    background-color: $nth-of-type-bg-color !important;
  }
  ._editor-height {
    min-height: 48px;
    width: 30px;
    border-left: 1px solid rgb(221, 221, 221);
  }
  ._string-editor {
    min-height: 72px;
  }
  ._instance-bg {
    background-color: white;
  }
  ._footer-button {
    position: absolute;
    bottom: 16px;
    left: 16px;
    .c-dropdown {
      display: inline-block;
    }
    .el-button {
      border-radius: 0;
    }
    .el-button + .el-button {
      margin: 0;
      margin-right: 8px;
    }
  }

  ._cell {
    border-top: 1px solid #ddd;
    border-left: 1px solid #ddd;
    box-sizing: border-box;
    font-size: 12px;
    line-height: 26px;
    flex: 1;
  }
  ._slot-parent {
    background: #ecf0f7;
  }
  ._constrain_no-data {
    height: 350px;
    border: 1px solid red;
  }
  ._name-column {
    padding: 0 8px;
  }
  ._value-column {
    padding: 0 8px;
    line-height: 26px;
    position: relative;
    border-right: 1px solid #ddd;

    ._slot-values {
      display: inline-flex;
      margin-right: 40px;
    }
    ._slot-add {
      display: inline-flex;
      position: absolute;
      right: 0;
      top: 0;
      width: 40px;
      height: 100%;
      ._add-img {
        position: absolute;
        right: 8px;
        top: 6px;
      }
    }
  }
  ._current-row {
    ._cell {
      box-shadow: inset -1px 0px 0px 0px #e0e0e0, inset 0px -1px 0px 0px #e0e0e0;
      border-top-color: $theme-color;
      border-left-color: $theme-color;
    }

    ._name-column {
      padding: 0 8px;

      // color: white;
    }
    ._value-column {
      box-sizing: border-box;
      line-height: 24px;
      border-right-color: $theme-color;
    }
  }
  ._current-row + ._table-row {
    line-height: 26px;
    ._cell {
      border-top-color: $theme-color;
    }
  }
  ._icons {
    width: 30px;
    border: 1px solid red;
  }
  ._table-row:last-of-type {
    ._cell {
      border-bottom: 1px solid #ddd;
    }
  }
  ._table-row._current-row:last-of-type {
    ._cell {
      border-bottom: 1px solid $theme-color;
    }
  }

  .el-input__inner {
    border: none;
    box-sizing: border-box;
    height: 24px;
    line-height: 24px;
    background: none;
    box-shadow: none;
    text-align: left;
  }
  .el-textarea__inner:focus {
    background: none;
    box-shadow: none;
  }
  .el-textarea__inner {
    background: none;
    box-shadow: none;
    padding: 5px 8px;
    border: none;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .el-input-border {
    border: none;
  }
  ._disabled {
    // color: #979797;
    cursor: not-allowed;
  }

  ._movebar {
    position: absolute;
    &._vertical {
      cursor: col-resize;
      width: 8px;
      height: 100%;
      top: 0;
    }
  }

  .el-icon-iconadd3 {
    background: url(/statics/icons/app/iconadd3.svg) center no-repeat;
    margin-right: 2px;
    width: 14px;
    height: 14px;
    background-size: cover;
  }

  .el-icon-iconuse {
    background: url(/statics/icons/app/iconuse.svg) center no-repeat;
    margin-right: 2px;
    width: 14px;
    height: 14px;
    background-size: cover;
  }

  .el-icon-icondisuse {
    background: url(/statics/icons/app/icondisuse.svg) center no-repeat;
    margin-right: 2px;
    width: 14px;
    height: 14px;
    background-size: cover;
  }
}
</style>
